﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facade;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.SalesOneWeb.SalesActivity
{
    public partial class VerificationSummary1 : SOSecurityPage, IntTest
    {
        string state = "";
        private int pageNo;
        private int pageN = 1;
        List<Policy> pol;
        List<User> app;
        int flag;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                StructureFacade fac = new StructureFacade();

                if (Page.Request.Params["SOCode"] != null)
                {

                    flag = 1;

                    string SOCode = Page.Request.Params["SOCode"].ToString();
                    string dateFrom = Page.Request.Params["DueFromDate"].ToString();
                    string dateto = Page.Request.Params["DueToDate"].ToString();

                    DateTime dueDateTo = Convert.ToDateTime(Page.Request.Params["DueToDate"].ToString());
                    DateTime dueDateFrom = DateTime.Parse(Page.Request.Params["DueFromDate"].ToString());
                    //DateTime dueDateTo = DateTime.Parse(Page.Request.Params["DueToDate"]);
                    state = "SOCode";



                    if (!IsPostBack)
                    {
                        pageNo = 1;
                        UCGrid1.SetText("1");
                        SearchSummeries(dueDateFrom, dueDateTo, SOCode, 1);
                        //quoList = fac.GetQuotationsBySocode(soCode, fromDate, toDate,1);
                    }
                    else
                    {
                        //quoList = fac.GetQuotationsBySocode(soCode, fromDate, toDate, pageNo);
                        SearchSummeries(dueDateFrom, dueDateTo, SOCode, pageNo);
                    }


                    #region commented
                    // pol = fac.GetPoliciesForDate(dueDateFrom, dueDateTo, SOCode);

                    //        if (pol.Count != 0)
                    //        {
                    //            int[] numbers = { 0, 1, 4, 5, 6, 7, 8, 10, 13, 14, 17, 18 };
                    //            lblResult.Visible = true;
                    //            lblResult.Text = "Verification policy: From : " + dateFrom + " To : " + dateto + " For SC: " + SOCode;
                    //            //lblResult.ForeColor = System.Drawing.Color.Blue;

                    //            UCGrid1.styleList = new List<GridViewStyle> {
                    //    new GridViewStyle{
                    //        ColumnId = "ContractId",
                    //        ColumnHeader = "Policy No.",
                    //        ColumnWidth = 80},
                    //    new GridViewStyle{
                    //        ColumnId = "CustomerId",
                    //        ColumnHeader = "Client Id",
                    //        ColumnWidth = 80},
                    //    new GridViewStyle{
                    //        ColumnId = "Suspence",
                    //        ColumnHeader = "Balance(Rs)",
                    //        Align="right",
                    //        ColumnWidth = 80
                    //        },
                    //    new GridViewStyle{
                    //        ColumnId = "Premium",
                    //        ColumnHeader = "Premium(Rs)",
                    //        ColumnWidth = 80,
                    //        Align="right"},
                    //    new GridViewStyle{
                    //        ColumnId = "DueDate",
                    //        ColumnHeader = "Due Date",
                    //        ColumnWidth = 100},
                    //     new GridViewStyle{
                    //        ColumnId = "PayMode",
                    //        ColumnHeader = "Pay Mode",
                    //        ColumnWidth = 80},
                    //     new GridViewStyle{
                    //        ColumnId = "LastUpdatedDate",
                    //        ColumnHeader = "Last Updated Date",
                    //        ColumnWidth = 120}
                    //};


                    //            UCGrid1.SetDataSource(pol, numbers);


                    //        }
                    //        else
                    //        {
                    //            //lblResult.Visible = true;
                    //            //lblResult.Text = "No results to display";
                    //            //lblResult.ForeColor = System.Drawing.Color.Red;

                    //            MassageSummery("IIS504");


                    //        } 
                    #endregion

                }

                if (Page.Request.Params["unitId"] != null && Page.Request.Params["Month"] != null && Page.Request.Params["Year"] != null)
                {
                    flag = 2;

                    int unitID = Convert.ToInt32(Page.Request.Params["unitId"].ToString());
                    int month = Convert.ToInt32(Page.Request.Params["Month"].ToString());
                    int year = Convert.ToInt32(Page.Request.Params["Year"].ToString());                    
                    string unitName = Page.Request.Params["UnitName"].ToString();
                    string branch = Page.Request.Params["Branch"];
                    string monthName = Page.Request.Params["monthName"];
                    

                    state = "Month";

                    //  List<SalesAppoinments> app = fac.GetActivitiesForSOcode(unitID,month,year);
                    //app = fac.GetUsersForSOcode(unitID, month, year);

                    if (!IsPostBack)
                    {
                        pageNo = 1;
                        UCGrid1.SetText("1");
                        SearchSummeries_1(unitID, month, year, 1);
                        //quoList = fac.GetQuotationsBySocode(soCode, fromDate, toDate,1);
                    }
                    else
                    {                        
                        SearchSummeries_1(unitID, month, year, pageN);
                    }            
               }                        

            }
            catch (Exception ex) { }
        }

        // get all policies for the given period for given SOCode
 
        public void SearchSummeries(DateTime dueDateFrm, DateTime dueDateto, string soCode, int pageN)
        {       
            StructureFacade fac = new StructureFacade();
            pol = fac.GetPoliciesForDate(dueDateFrm, dueDateto, soCode, pageN);
            string SOCode = Page.Request.Params["SOCode"].ToString();
            string dateFrom = Page.Request.Params["DueFromDate"].ToString();
            string dateto = Page.Request.Params["DueToDate"].ToString();

            if (pol.Count != 0)
            {
                int[] numbers = { 0,1,9,10,11,13,14,15,16,17,18 };
                lblResult.Visible = true;
                lblResult.Text = "Verification policy: From : " + dateFrom + " To : " + dateto + " For SC: " + SOCode;

                //lblResult.ForeColor = System.Drawing.Color.Blue;

                UCGrid1.styleList = new List<GridViewStyle> {

                new GridViewStyle{
                    ColumnId = "ContractId",
                    ColumnHeader = "Policy No.",
                    ColumnWidth = 80},

                new GridViewStyle{
                    ColumnId = "CustomerId",
                    ColumnHeader = "Client Id",
                    ColumnWidth = 80},

                new GridViewStyle{
                    ColumnId = "Suspence",
                    ColumnHeader = "Balance(Rs)",
                    Align="right",
                    ColumnWidth = 80
                    },

                new GridViewStyle{
                    ColumnId = "PolicyState",
                    ColumnHeader = "Policy Status",                    
                    ColumnWidth = 100                    
                    },

                new GridViewStyle{
                    ColumnId = "Premium",
                    ColumnHeader = "Premium(Rs)",
                    ColumnWidth = 80,
                    Align="right"},
                new GridViewStyle{
                    ColumnId = "DueDate",
                    ColumnHeader = "Due Date",
                    ColumnWidth = 100},
                 new GridViewStyle{
                    ColumnId = "PayMode",
                    ColumnHeader = "Pay Mode",
                    ColumnWidth = 80},
                 new GridViewStyle{
                    ColumnId = "LastUpdatedDate",
                    ColumnHeader = "Last Updated Date",
                    ColumnWidth = 120}
            };

                if (pol.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                {
                    UCGrid1.IsLastPage = (fac.GetPoliciesForDate(dueDateFrm, dueDateto, soCode, pageN + 1)).Count > 0 ? false : true;
                }
                UCGrid1.SetDataSource(pol, numbers);
                //UCGrid1.SetDataSource(pol);


            }
            else
            {               
                MassageSummery("IIS504");
            }
        }

        //Get all users who didn't submit for given time period

        public void SearchSummeries_1(int unitId, int month, int year, int pageN)
        {

            int unitID = Convert.ToInt32(Page.Request.Params["unitId"].ToString());            
            string unitName = Page.Request.Params["UnitName"].ToString();
            string branch = Page.Request.Params["Branch"];
            string monthName = Page.Request.Params["monthName"];

            
            StructureFacade fac = new StructureFacade();
            app = fac.GetUsersForSOcode(unitId, month, year, pageN);

            if (app.Count != 0)
            {
                int[] numbers = { 0, 1, 2, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };

                lblResult.Visible = true;
                lblResult.Text = "Verification-    Branch " + branch + "    Unit " + unitName + "   SC not submitted for " + monthName + " " + year;

                UCGrid1.styleList = new List<GridViewStyle> {
                new GridViewStyle{
                    ColumnId = "SoCode",
                    ColumnHeader = "SO Code",
                    ColumnWidth = 180},
                new GridViewStyle{
                    ColumnId = "Name",
                    ColumnHeader = "Name",
                    ColumnWidth = 180}
                
            };
                if (app.Count == Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]))
                {
                    UCGrid1.IsLastPage = (fac.GetUsersForSOcode(unitId, month, year, pageN + 1)).Count > 0 ? false : true;
                }
                UCGrid1.SetDataSource(app, numbers);
                //UCGrid1.SetDataSource(app);


            }
            else
            {
                //lblResult.Visible = true;
                //lblResult.Text = "No results to display";
                //lblResult.ForeColor = System.Drawing.Color.Red;

                MassageSummery("IIS504");

            }
        }




        

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/SalesActivity/SearchVerificationSummary.aspx");
        }

        #region IntTest Members

        public void Next()
        {          
            
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) + 1;
                UCGrid1.SetText(pageNo.ToString());

                GetFunction();
            }
            else
            {
                pageNo = 2;
                UCGrid1.SetText("2");
            }

            GetFunction();
            
        }

        public void Previous()
        {
            if (UCGrid1.GetText() != "1")
            {
                pageNo = Convert.ToInt32(UCGrid1.GetText()) - 1;
                UCGrid1.SetText(pageNo.ToString());

                GetFunction();
            }
        }

        public void Go()
        {
            int p = int.Parse(UCGrid1.GetText());
            if (p != 0)
            {
                try
                {
                    if (flag == 1)
                    {
                        DateTime dueDateTo = Convert.ToDateTime(Page.Request.Params["DueToDate"].ToString());
                        DateTime dueDateFrom = DateTime.Parse(Page.Request.Params["DueFromDate"].ToString());
                        string SOCode = Page.Request.Params["SOCode"].ToString();

                        SearchSummeries(dueDateFrom, dueDateTo, SOCode, p);
                    }
                    else if (flag == 2)
                    {
                        int unitID = Convert.ToInt32(Page.Request.Params["unitId"].ToString());
                        int month = Convert.ToInt32(Page.Request.Params["Month"].ToString());
                        int year = Convert.ToInt32(Page.Request.Params["Year"].ToString());

                        SearchSummeries_1(unitID, month, year, p);
                    }

                    pageNo = p;
                }
                catch (Exception)
                {

                    throw;
                }


            }
        }

        #endregion


        //set grids for relavant functions

        public void GetFunction()
        {
            if (flag == 1)
            {
                DateTime dueDateTo = Convert.ToDateTime(Page.Request.Params["DueToDate"].ToString());
                DateTime dueDateFrom = DateTime.Parse(Page.Request.Params["DueFromDate"].ToString());
                string SOCode = Page.Request.Params["SOCode"].ToString();

                SearchSummeries(dueDateFrom, dueDateTo, SOCode, pageNo);
            }
            else if (flag == 2)
            {
                int unitID = Convert.ToInt32(Page.Request.Params["unitId"].ToString());
                int month = Convert.ToInt32(Page.Request.Params["Month"].ToString());
                int year = Convert.ToInt32(Page.Request.Params["Year"].ToString());

                SearchSummeries_1(unitID, month, year, pageNo);
            }
        }




       
    }
}
